package cn.iocoder.yudao.module.hs.job;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.quartz.core.handler.JobHandler;
import cn.iocoder.yudao.module.hs.controller.admin.picktask.vo.PickTaskSaveReqVO;
import cn.iocoder.yudao.module.hs.controller.admin.rfidchangerecord.vo.RfidChangeRecordSaveReqVO;
import cn.iocoder.yudao.module.hs.dal.dataobject.picktask.PickTaskDO;
import cn.iocoder.yudao.module.hs.dal.dataobject.rfid.RfidDO;
import cn.iocoder.yudao.module.hs.dal.dataobject.tray.TrayDO;
import cn.iocoder.yudao.module.hs.dal.dataobject.warehouse.WarehouseDO;
import cn.iocoder.yudao.module.hs.dal.mysql.rfid.RfidMapper;
import cn.iocoder.yudao.module.hs.enums.picktask.PickTaskStatusEnum;
import cn.iocoder.yudao.module.hs.enums.picktask.PickTaskTypeEnum;
import cn.iocoder.yudao.module.hs.enums.warehouse.WarehouseTypeEnum;
import cn.iocoder.yudao.module.hs.netty.RFIDData;
import cn.iocoder.yudao.module.hs.service.picktask.PickTaskService;
import cn.iocoder.yudao.module.hs.service.rfid.RfidService;
import cn.iocoder.yudao.module.hs.service.tray.TrayService;
import cn.iocoder.yudao.module.hs.service.warehouse.WarehouseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Component
@Slf4j
public class LowBatteryJob implements JobHandler {

    @Resource
    private RfidService rfidService;

    @Resource
    private TrayService trayService;

    @Resource
    private PickTaskService pickTaskService;

    @Resource
    private WarehouseService warehouseService;

    @Resource
    private RfidMapper rfidMapper;

    @Override
    public String execute(String param) {
        log.info("## 低电量RFID扫描任务开始：");
        List<RFIDData> rfidList = RFIDData.getLastRFIDList();
        if (CollUtil.isEmpty(rfidList)) {
            log.info("## RFID扫描故障，未获取RFID上报数据");
        }
        int count = 0;
        for (RFIDData rfidData : rfidList) {
            if (rfidData.getIsLow().equals(1)) {
                RfidDO rfid = rfidService.getRfid(rfidData.getRfidCode());
                if (rfid == null) {
                    continue;
                }
                TrayDO tray = trayService.getTray(rfid.getTrayCode());
                if (tray == null) {
                    continue;
                }
                WarehouseDO warehouse = warehouseService.getWarehouse(tray.getWarehouseId());
                if (warehouse == null) {
                    continue;
                }
                List<PickTaskDO> lowBatteryPickTask = pickTaskService.getLowBatteryPickTask(rfidData.getRfidCode());
                if (CollUtil.isNotEmpty(lowBatteryPickTask)) {
                    continue;
                }
                if (WarehouseTypeEnum.PRODUCTION.getType().equals(warehouse.getWarehouseType())) {
                    // 创建更换标签任务
                    PickTaskSaveReqVO createReqVO = new PickTaskSaveReqVO();
                    createReqVO.setRfidCode(rfid.getCode());
                    createReqVO.setTaskType(PickTaskTypeEnum.CHANGE_RFID.getType());
                    createReqVO.setStatus(PickTaskStatusEnum.TO_BE_PICKED.getStatus());
                    createReqVO.setWarehouseType(warehouse.getWarehouseType());
                    createReqVO.setWarehouseId(tray.getWarehouseId());
                    createReqVO.setAssignUserId(null);
                    createReqVO.setAssignUserName("所有人");
                    createReqVO.setWarehouseId(warehouse.getId());
                    createReqVO.setDeptId(101L);
                    pickTaskService.createPickTask(createReqVO);

                    rfid.setLowBattery(true);
                    rfidMapper.updateById(rfid);
                    count++;
                }
            }
        }
        log.info("## 低电量RFID扫描任务结束：{}", count);
        return "## 低电量RFID扫描任务结束：" + count;
    }

}
